/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : MqcpDeviceInfoReport.c
  * @brief          : MqcpDeviceInfoReport
  ******************************************************************************
  * @attention
  * 本文件用于  用户信息上报
  * @20210414:文件模板第一次生成
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include "MqcpTaskGet.h"
#include "SystemConfigTask.h"

static MqcpDataDef *MqcpTaskGetReqProcess(MqcpMessageDef *Message);
static MqcpDataDef *MqcpTaskGetRespProcess(MqcpMessageDef *Message);

void TaskGetInit(void)//用户信息上报初始化
{
	MqcpRouterDef routeReq = 
	{
		1,
		TaskGetReq,
		MqcpTaskGetReqProcess
	};
	while(addRouter(routeReq));
	
	MqcpRouterDef routeResp = 
	{
		1,
		TaskGetResp,
		MqcpTaskGetRespProcess
	};
	while(addRouter(routeResp));
}


MqcpDataDef *MqcpTaskGetReqProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	MqcpData->isNeedResp = Message->Sync;
	MqcpData->Size = 0;
	
	return MqcpData;
}

MqcpDataDef *MqcpTaskGetRespProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	sTimeListWorkDef *MqcpTimeList = (sTimeListWorkDef *)(&(MqcpDataType.Modbus_TIM[0]));
	SysDataDef *SysData = (SysDataDef *)(MqcpDataType.Modbus_SYS);
	
	MqcpData->isNeedResp = 0;
	MqcpData->Size = 0;
	
	uint16_t MqcpSize = 0;//发送的data数
	
	rt_memcpy(MqcpData->MqcpMessage.MsgId,Message->MsgId,12);MqcpData->Size = MqcpData->Size +12;
	MqcpData->MqcpMessage.Sync = MqcpSyncFalse;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Code = MqcpCodeOK;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Ver = MQCP_USE_VER;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.DevID[3] = MqcpDevice.DevId>>24;
	MqcpData->MqcpMessage.DevID[2] = MqcpDevice.DevId>>16;
	MqcpData->MqcpMessage.DevID[1] = MqcpDevice.DevId>>8;
	MqcpData->MqcpMessage.DevID[0] = MqcpDevice.DevId&0xff;MqcpData->Size = MqcpData->Size + 4;
	MqcpData->MqcpMessage.Cmd = TaskGetResp;MqcpData->Size = MqcpData->Size + 1;
	
	MqcpData->MqcpMessage.Data[MqcpSize] = ((uint16_t)(MqcpTimeList->TimeListSize))&0xff;MqcpData->Size = MqcpData->Size + 1;MqcpSize = MqcpSize + 1;
	MqcpData->MqcpMessage.Data[MqcpSize] = ((uint16_t)(MqcpTimeList->TimeListSizeMax))&0xff;MqcpData->Size = MqcpData->Size + 1;MqcpSize = MqcpSize + 1;
	
	for(uint16_t i = 0; i < MqcpTimeList->TimeListSizeMax; i++)
	{
		if(0 != MqcpTimeList->TimeList[i].Id)
		{
			MqcpData->MqcpMessage.Data[MqcpSize + 0] = ((uint16_t)(MqcpTimeList->TimeList[i].Id))&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize + 1] = ((uint16_t)(MqcpTimeList->TimeList[i].Enable))&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize + 2] = ((((uint16_t)(MqcpTimeList->TimeList[i].onHours))+24-SysData->Time_zone)%24)&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize + 3] = ((uint16_t)(MqcpTimeList->TimeList[i].onMinutes))&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize + 4] = ((((uint16_t)(MqcpTimeList->TimeList[i].offHours))+24-SysData->Time_zone)%24)&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize + 5] = ((uint16_t)(MqcpTimeList->TimeList[i].offMinutes))&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize + 6] = GetWeek(MqcpTimeList->TimeList[i].onHours, MqcpTimeList->TimeList[i].Week);
			MqcpData->MqcpMessage.Data[MqcpSize + 7] = ((uint16_t)(MqcpTimeList->TimeList[i].Type))&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize + 9] = ((uint16_t)(MqcpTimeList->TimeList[i].RegAddress))>>8;
			MqcpData->MqcpMessage.Data[MqcpSize + 8] = ((uint16_t)(MqcpTimeList->TimeList[i].RegAddress))&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize + 11] = ((uint16_t)(MqcpTimeList->TimeList[i].OpenData))>>8;
			MqcpData->MqcpMessage.Data[MqcpSize + 10] = ((uint16_t)(MqcpTimeList->TimeList[i].OpenData))&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize + 13] = ((uint16_t)(MqcpTimeList->TimeList[i].CloseData))>>8;
			MqcpData->MqcpMessage.Data[MqcpSize + 12] = ((uint16_t)(MqcpTimeList->TimeList[i].CloseData))&0xff;MqcpData->Size = MqcpData->Size + 14;MqcpSize = MqcpSize + 14;
		}
	}
	
	MqcpData->MqcpMessage.Len[1] = MqcpSize>>8;
	MqcpData->MqcpMessage.Len[0] = MqcpSize&0xff;MqcpData->Size = MqcpData->Size + 2;
	
	MqcpData->MqcpMessage.Data[MqcpSize] = Chack_Xor((uint8_t*)(&(MqcpData->MqcpMessage)),MqcpData->Size);MqcpData->Size = MqcpData->Size + 1;
	
	return MqcpData;
}


/************************ (C) COPYRIGHT CunJu *****END OF FILE****/
